﻿/*
 * Created by SharpDevelop.
 * User: think
 * Date: 2011/11/18
 * Time: 22:39
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;


/// <summary>
/// Description of MainForm.
/// </summary>
public partial class frmMain : BaseWinFrom
{
    public frmMain()
    {
        //
        // The InitializeComponent() call is required for Windows Forms designer support.
        //
        InitializeComponent();

        //
        // TODO: Add constructor code after the InitializeComponent() call.
        //

        AutoInitializeComponent();
    }

    void AutoInitializeComponent()
    {
        int i = 0;

        this.Padding = new Padding(10);

        if (Core.Session.Certificate != null)
        {
            if (Core.Session.Certificate.HasPermission(Model.Certificate.Permission.Certificate))
            {
                this.Controls.Add(GetControlButton(i, Model.Certificate.Permission.Certificate));
                i++;
            }
            if (Core.Session.Certificate.HasPermission(Model.Certificate.Permission.Diagnosis))
            {
                this.Controls.Add(GetControlButton(i, Model.Certificate.Permission.Diagnosis));
                i++;
            }
            if (Core.Session.Certificate.HasPermission(Model.Certificate.Permission.Drugs))
            {
                this.Controls.Add(GetControlButton(i, Model.Certificate.Permission.Drugs));
                i++;
            }
            if (Core.Session.Certificate.HasPermission(Model.Certificate.Permission.UserBase))
            {
                this.Controls.Add(GetControlButton(i, Model.Certificate.Permission.UserBase));
                i++;
            }
        }
        {
            this.Controls.Add(GetControlButton(i, Model.Certificate.Permission.None));
            i++;
        }

        this.Height = i * CONTROLBUTTONHEIGHT + this.Padding.Top + this.Padding.Bottom + 20;
        this.Width = CONTROLBUTTONWIDTH + this.Padding.Left + this.Padding.Right+10;
    }



    const int CONTROLBUTTONHEIGHT = 40;
    const int CONTROLBUTTONWIDTH = 100;

    private Control GetControlButton(int Index, Model.Certificate.Permission Permission)
    {
        Button btReturn = new Button();
        btReturn.Size = new Size(CONTROLBUTTONWIDTH, CONTROLBUTTONHEIGHT);
        btReturn.Location = new Point(this.Padding.Left, this.Padding.Top + Index * btReturn.Height);
        switch (Permission)
        {
            case Model.Certificate.Permission.None:
                btReturn.Text = "退出系统";
                btReturn.Click += new EventHandler(ButtonExit_Click);
                break;
            case Model.Certificate.Permission.Certificate:
                btReturn.Text = "用户管理";
                btReturn.Click += new EventHandler(ButtonCertificate_Click);
                break;
            case Model.Certificate.Permission.Diagnosis:
                btReturn.Text = "病例管理";
                btReturn.Click += new EventHandler(ButtonDiagnosis_Click);
                break;
            case Model.Certificate.Permission.Drugs:
                btReturn.Click += new EventHandler(ButtonDrugs_Click);
                btReturn.Text = "药剂管理";
                break;
            case Model.Certificate.Permission.UserBase:
                btReturn.Click += new EventHandler(ButtonUserBase_Click);
                btReturn.Text = "客户管理";
                break;
        }
        return btReturn;
    }

    void ButtonCertificate_Click(object sender, EventArgs e)
    {
        new frmCertificateList().Show();
    }
    
    void ButtonDiagnosis_Click(object sender,EventArgs e)
    {
    	new frmDiagnosisList().Show();
    }

    void ButtonDrugs_Click(object sender, EventArgs e)
    {
        new frmDrugsList().Show();
    }

    void ButtonUserBase_Click(object sender, EventArgs e)
    {
        new frmUserList().Show();
    }

    void ButtonExit_Click(object sender, EventArgs e)
    {
        OnClosing(new System.ComponentModel.CancelEventArgs(true));
    }

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        if (MessageBox.Show("确定退出", "确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            e.Cancel = false;
            Application.Exit(e);
        }
        else
        {
            e.Cancel = true;
        }
        base.OnClosing(e);
    }
}